package testcase;

import io.ConsoleIO;
import io.IOManager;
import junit.framework.TestCase;
import game.GameManager;
import java.io.*;
import java.util.NoSuchElementException;

import validation.Bomb;
import validation.FourWithDualPair;
import validation.FourWithDualSolo;
import validation.Nuke;
import validation.Pair;
import validation.PairChain;
import validation.Solo;
import validation.SoloChain;
import validation.Trio;
import validation.TrioChain;
import validation.TrioWithPair;
import validation.TrioWithPairChain;
import validation.TrioWithSolo;
import validation.TrioWithSoloChain;
import validation.Type;

import card.Card;
import card.Cardset;

// This is an integration test for GameManager
public class GameManagerTest extends TestCase{
	
	class IOStub extends ConsoleIO{
		public String lastPrint;
		
		@Override
		public void println() {					
		}

		@Override
		public void println(String s) {
			lastPrint = s;	  		
		}

		@Override
		public void print(String s) {
			lastPrint = s;			
		}

		@Override
		public void print(Card c, int index) {						
		}

		@Override
		public void print(Cardset cs) {						
		}		
	}
	
	public void setUp(){
		Type type;
		type = Nuke.getInstance();
		type.clear();
		type = Bomb.getInstance();
		type.clear();
		type = Solo.getInstance();
		type.clear();
		type = Pair.getInstance();
		type.clear();
		type = Trio.getInstance();
		type.clear();
		type = SoloChain.getInstance();
		type.clear();
		type = PairChain.getInstance();
		type.clear();
		type = TrioChain.getInstance();
		type.clear();
		type = TrioWithSoloChain.getInstance();
		type.clear();
		type = TrioWithPairChain.getInstance();
		type.clear();
		type = TrioWithSolo.getInstance();
		type.clear();
		type = TrioWithPair.getInstance();
		type.clear();
		type = FourWithDualSolo.getInstance();
		type.clear();
		type = FourWithDualPair.getInstance();
		type.clear();
		
	}

	
	public void testOneGame()
	{
		String input = "1 3 ";
		for (int i=0;i<19;i++)
		{
			input += "0 t c t c t c ";
		}
		
		input += "0 t c n";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		
		IOStub io = new IOStub();
		IOManager ioManager = new IOManager(io);
		
		GameManager manager = new GameManager(ioManager);
		manager.startGame();
		
		assertEquals("== End ==",io.lastPrint);		
	}
	
	public void testTwoGame()
	{
		String input = "3 1 ";
		for (int i=0;i<19;i++)
		{
			input += "0 t c t c t c ";
		}
		
		input += "0 t c y";
		
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		System.setOut(new PrintStream(new ByteArrayOutputStream()));
		GameManager manager = new GameManager();
		
		boolean bGameEnd = true;
		
		try {			
			manager.startGame();
		} catch (NoSuchElementException e) {
			bGameEnd = false;
		}
					
		assertEquals(bGameEnd,false);			
	}

	
}
